package com.css.apps.base.common.hessian;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.css.apps.base.common.Constants;
import com.css.apps.base.common.hessian.model.HessianServiceInfo;
import com.css.util.ListUtil;
import com.css.util.StringHelper;

public enum HessianConfig {

	INSTANCE;

	public Map<String, HessianServiceInfo> getServices() {
		return INSTANCE.services;
	}
	
	private Log log = LogFactory.getLog(HessianConfig.class);
	private Map<String, HessianServiceInfo> services;

	private HessianConfig() {
		if (services == null) {
			SAXReader reader = new SAXReader();
			try {
				Document document = reader.read(HessianConfig.class.getResourceAsStream("/hessianconfig.xml"));
				Element root = document.getRootElement();
				if (root != null) {
					services = traverseHessianConfig(root);
					System.out.println(services);
				}
			} catch (DocumentException e) {
				log.error("装载系统配置文件【hessianconfig.xml】失败", e);
				e.printStackTrace();
			}
		}
	}

	@SuppressWarnings("unchecked")
	private Map<String, HessianServiceInfo> traverseHessianConfig(Element root) {
		Map<String, HessianServiceInfo> services = null;
		List<Element> elements = root.elements();
		if (!ListUtil.isLstEmpty(elements)) {
			services = new HashMap<>();
			for (Element element : elements) {
				HessianServiceInfo hs = null;
				String openFlag = element.attribute("openFlag").getStringValue();
				if (StringHelper.isNotEmpty(openFlag) && Constants.YES.equals(openFlag)) {
					List<Attribute> attrList = element.attributes();
					Map<String, String> attrMap = traverseAttr(attrList);
					String serviceId = attrMap.get("id");
					hs = new HessianServiceInfo();
					hs.setAttributes(attrMap);
					hs.setOpenFlag(openFlag);
					hs.setServiceId(serviceId);
					hs.setServiceUrls(getServiceUrls(element));
					services.put(serviceId, hs);
				}
			}
		}
		return services;
	}

	@SuppressWarnings("unchecked")
	private List<String> getServiceUrls(Element element) {
		List<String> serviceUrls = null;
		if (element != null) {
			serviceUrls = new ArrayList<>();
			List<Element> urlEles = element.elements();
			if (!ListUtil.isLstEmpty(urlEles)) {
				for (Element ele : urlEles) {
					if ("url".equals(ele.getName())
							&& Constants.YES.equals(ele.attribute("openFlag").getStringValue())) {
						serviceUrls.add(ele.getStringValue());
					}
				}
			}
		}
		return serviceUrls;
	}

	private Map<String, String> traverseAttr(List<Attribute> attrList) {
		Map<String, String> attrMap = null;
		if (!ListUtil.isLstEmpty(attrList)) {
			attrMap = new HashMap<>();
			for (Attribute attr : attrList) {
				String attrName = attr.getName();
				String attrVal = attr.getValue();
				if (StringHelper.isNotEmpty(attrName)) {
					attrMap.put(attrName, attrVal);
				}
			}
		}
		return attrMap;
	}

}
